var table = layui.table
var form = layui.form
var layer = layui.layer
var upload = layui.upload
var element = layui.element

//初始化表格数据
function initBaseTable(options) {
  layui.table.render({
    id: 'mytable',
    elem: '#mytable',
    toolbar: '#toolbar',
    url: options.getUrl,
    page: true,
    loading: true,
    height: 'full-200',
    limit: 15,
    limits: [15, 30, 50, 100],
    cols: [options.cols]
  })
}

function destroyModel(options) {
  $.deleteForm({
    url: options.delUrl,
    param: { id: options.id },
    success: function (result) {
      parent.layer.alert(result.message, function () {
        location.reload()
      })
    }
  })
  return true
}

function clearCache() {
  var index
  $.ajax({
    url: '/clearCache',
    type: 'post',
    dataType: 'json',
    beforeSend: function () {
      index = layer.load()
    },
    success: function (result) {
      layer.alert(result.message, '系统提示')
    },
    complete: function () {
      layer.close(index)
    },
    error: function (xmlHttpRequest, textStatus, errorThrown) {
      layer.msg('error：' + errorThrown)
    }
  })
}

//格式化DateTime
function formatDateTime(val) {
  var date = new Date(val)
  return date.Format('yyyy-MM-dd hh:mm')
}

$.reload = function () {
  location.reload()
  return false
}

$.loading = function (bool, text) {
  var $loadingpage = top.$('#loadingPage')
  var $loadingtext = $loadingpage.find('.loading-content')
  if (bool) {
    $loadingpage.show()
  } else {
    if ($loadingtext.attr('istableloading') == undefined) {
      $loadingpage.hide()
    }
  }
  if (!!text) {
    $loadingtext.html(text)
  } else {
    $loadingtext.html('数据加载中，请稍后…')
  }
  $loadingtext.css('left', (top.$('body').width() - $loadingtext.width()) / 2 - 50)
  $loadingtext.css('top', (top.$('body').height() - $loadingtext.height()) / 2)
}

$.modalAlert = function (content, type) {
  var icon = 3
  if (type === 'warning') {
    icon = 0
  }
  if (type === 'success') {
    icon = 1
  }
  if (type === 'error') {
    icon = 2
  }
  top.layer.alert(content, {
    icon: icon,
    title: '系统提示',
    btn: ['确认'],
    btnclass: ['btn btn-primary']
  })
}

$.modalMsg = function (content, type) {
  if (type != undefined) {
    var icon = 3
    if (type === 'warning') {
      icon = 0
    }
    if (type === 'success') {
      icon = 1
    }
    if (type === 'error') {
      icon = 2
    }
    top.layer.msg(content, { icon: icon, time: 3000, shift: 5, offset: '80%' })
    top
      .$('.layui-layer-msg')
      .find('i.' + icon)
      .parents('.layui-layer-msg')
      .addClass('layui-layer-msg-' + type)
  } else {
    top.layer.msg(content)
  }
}

$.modalConfirm = function (content, callBack) {
  top.layer.confirm(
    content,
    {
      icon: 0,
      title: '系统提示',
      btn: ['确认', '取消'],
      btnclass: ['btn btn-primary', 'btn btn-danger']
    },
    function () {
      callBack(true)
    },
    function () {
      callBack(false)
    }
  )
}

$.ajaxSubmit = function (options) {
  var defaults = {
    url: '',
    data: {},
    type: 'POST',
    async: false,
    dataType: 'JSON',
    success: null,
    isClose: true,
    isCallBack: null
  }
  options = $.extend(defaults, options)
  var index
  $.ajax({
    url: options.url,
    data: options.data,
    type: options.type,
    async: options.async,
    dataType: options.dataType,
    beforeSend: function () {
      index = layer.load(2)
    },
    success: function (result) {
      if (options.isCallBack === true) {
        options.success(result)
      } else {
        if (result.success) {
          if (options.isClose === true) {
            parent.layer.alert(result.message, function () {
              location.href = window.modelInfo.home
            })
          } else {
            parent.layer.alert(result.message)
          }
        } else {
          parent.layer.alert(result.message)
        }
      }
    },
    complete: function () {
      parent.layer.close(index)
    },
    error: function (xmlHttpRequest, textStatus, errorThrown) {
      parent.layer.close(index)
      $.modalMsg(errorThrown, 'error')
    }
  })
}

$.deleteForm = function (options) {
  var defaults = {
    prompt: '注：您确定要删除该项数据吗？',
    url: '',
    param: {},
    loading: '正在删除数据...',
    success: null,
    close: true
  }
  options = $.extend(defaults, options)
  $.modalConfirm(options.prompt, function (r) {
    if (r) {
      $.ajax({
        url: options.url,
        data: options.param,
        type: 'post',
        dataType: 'json',
        success: function (result) {
          options.success(result)
          //if (result.message) {
          //    options.success(result);
          //    $.modalMsg(result.message, "success");
          //} else {
          //    $.modalAlert(result.message, "warning");
          //}
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
          $.loading(false)
          $.modalMsg(errorThrown, 'error')
        },
        beforeSend: function () {
          $.loading(true, options.loading)
        },
        complete: function () {
          $.loading(false)
        }
      })
    }
  })
}

//过于16点就会跳掉第二天，在不精确到时分秒的情况下可以用
Date.prototype.Format = function (fmt) {
  //author: meizz
  var o = {
    'M+': this.getMonth() + 1, //月份
    'd+': this.getDate(), //日
    'h+': this.getHours(), //小时
    'm+': this.getMinutes(), //分
    's+': this.getSeconds(), //秒
    'q+': Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds() //毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) if (o.hasOwnProperty(k)) if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  return fmt
}
